// pages/shop/shop.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cat_id:'',
    sele_check:"请选择价格区间",
    sele_num: { price_min: '', price_max:''},
    select_bool:false,
    state: false,
    first_click: false,
    option_list:[],
    cat_list: [
                { name: '8万以下', minprice: '0', maxprice: '80000' },
                { name: '8-15万', minprice: '80000', maxprice: '150000' },
                { name: '15万以上', minprice: '150000', maxprice:''},
              ],
    biao_list: [
      { name: '2万以下', minprice: '0', maxprice: '20000' },
      { name: '2-3万', minprice: '20000', maxprice: '30000' },
      { name: '3万以上', minprice: '30000', maxprice:''},
    ],
  },
  toggle: function () {
    var list_state = this.data.state,
      first_state = this.data.first_click;
    if (!first_state) {
      this.setData({
        first_click: true
      });
    }
    if (list_state) {
      this.setData({
        state: false
      });
    } else {
      this.setData({
        state: true
      });
    }
  },
  select_option:function(e){
    var json = { price_min: '', price_max: '' };
    json.price_min = e.currentTarget.dataset.minprice;
    json.price_max = e.currentTarget.dataset.maxprice;
    this.setData({
      sele_check:e.currentTarget.dataset.value,
      sele_num:json
    })
    
   
    this.toggle()
  },
  // 去商品详情
  go_pro_detail:function(e){

    wx.navigateTo({
      url: '../product_detail/product_detail?id='+e.currentTarget.dataset.id,
    })
  },
  // 点击分类
  change_tab:function(e){
    wx.showLoading({
      title: '加载中',
    })
    var that = this;
    var cat_list = that.data.catlist;
    var id;
    cat_list.map((item, i) => {
      item.active = false;
      if (i == e.currentTarget.dataset.index){
        item.active = true;
        id = item.cat_id
      }
    })
    if(id == 1){
      that.setData({
        option_list:that.data.cat_list
      })
    }else if(id == 2){
      that.setData({
        option_list: that.data.biao_list
      })
      
    }
    that.setData({
      catlist: cat_list,
      sele_check: "请选择价格区间",
      sele_num: { price_min: '', price_max: '' }
    })

    wx.request({
      url: app.globalData.url+'category.php?id='+id,
      success:function(res){
        console.log(res)
        that.setData({
          pro_list:res.data,
          cat_id: id
        })
        wx.hideLoading()
      }
    })
  },
  min_price:function(e){
    var that = this;
    console.log(e.detail.value)
    var reg = /^\d+(\.{0,1}\d+){0,1}$/;
    if (reg.test(e.detail.value)){
      that.setData({
        minprice: Number(e.detail.value)
      })
    }else{
      that.setData({
        minprice: ""
      })
      wx.showToast({
        title: '请输入大于等于0的数字',
        icon:"none"
      })
    }
  },
  max_price:function(e){
    var that = this;
    console.log(e.detail.value)
    var reg = /^\d+(\.{0,1}\d+){0,1}$/;
    if (reg.test(e.detail.value)) {
      that.setData({
        maxprice: Number(e.detail.value)
      })
    } else {
      that.setData({
        maxprice: ""
      })
      wx.showToast({
        title: '请输入大于等于0的数字',
        icon: "none"
      })
    }
  },
  // 搜索
  seach:function(){
    wx.showLoading({
      title: '搜索中',
    })
    var that = this;
    if (that.data.sele_num.price_max == "" && that.data.sele_num.price_min == "" ){
      wx.showToast({
        title: '请选择价格区间',
        icon:"none"
      })
      return false
    }
    wx.request({
      url: app.globalData.url + 'category.php?id=' + that.data.cat_id,
      data:{
        price_min: that.data.sele_num.price_min,
        price_max: that.data.sele_num.price_max
      },
      success:function(res){
        console.log(that.data.sele_num)
        that.setData({
          pro_list:res.data
        })
        wx.hideLoading()
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var id;
     // 加载分类列表
     wx.showLoading({
       title: '加载中',
     })
     that.setData({
       option_list: that.data.cat_list
     })
    wx.request({
      url: app.globalData.url+'ny_flow.php?action=get_category',
      success:function(res){
        console.log(res)
        var cat_list = res.data.slice(0,2);
        console.log(cat_list)
        cat_list.map((item,i)=>{
          console.log(item)
          if(i == 0){
            item.active = true;
            id = item.cat_id;
          }else{
            item.active = false;
          }
        })
        // 加载分类商品列表
        wx.request({
          url: app.globalData.url + 'category.php?id=' + id,
          success: function (res) {
            console.log(res)
            that.setData({
              pro_list: res.data
            })
            wx.hideLoading()
          }
        })
        console.log(cat_list)
        that.setData({
          catlist: cat_list,
          cat_id:id
        })
      }
    })
    wx.request({
      url: app.globalData.url + 'ny_flow_goods.php?action=zuiyizhang',
      success: function (res) {
        console.log(res)
        that.setData({
          luesuo:res.data
        })

      }
    })
    

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
   var that = this;
    return {
      title: '鸿浪保证质量车正品表！',
      path: '../shop/shop',
      imageUrl: that.data.luesuo

    }
   
  }
})